import {
    createRouter,
    createWebHistory,
    RouteLocationNormalized,
} from "vue-router";
import store from "../store";
const Home = () =>
    import ("../views/home/Home");
const Category = () =>
    import ("../views/category/Category");
const Detail = () =>
    import ("../views/detail/Detail");
const Profile = () =>
    import ("../views/profile/Profile");
const Register = () =>
    import ("../views/profile/Register");
const Login = () =>
    import ("../views/profile/Login");
const Shopcart = () =>
    import ("../views/shopcart/Shopcart");
const AddressList = () =>
    import ("../views/profile/AddressList");
const AddressEdit = () =>
    import ("../views/profile/AddressEdit");
const createOrder = () =>
    import ("../views/order/createOrder");

const routes = [{
        path: "/",
        name: "DefaultHome",
        component: Home,
        meta: {
            title: "图书商城-首页",
        },
    },
    {
        path: "/home",
        name: "Home",
        component: Home,
        meta: {
            title: "图书商城-首页",
        },
    },
    {
        path: "/category",
        name: "Category",
        component: Category,
        meta: {
            title: "图书商城-商品分类",
        },
    },
    {
        path: "/detail",
        name: "Detail",
        component: Detail,
        meta: {
            title: "图书商城-商品详情",
        },
    },
    {
        path: "/profile",
        name: "Profile",
        component: Profile,
        meta: {
            title: "图书商城-个人中心",
            isAuthRequired: true,
        },
    },
    {
        path: "/shopcart",
        name: "Shopcart",
        component: Shopcart,
        meta: {
            title: "图书商城-购物车",
            isAuthRequired: true,
        },
    },
    {
        path: "/register",
        name: "Register",
        component: Register,
        meta: {
            title: "注册",
        },
    },
    {
        path: "/login",
        name: "Login",
        component: Login,
        meta: {
            title: "登录",
        },
    },
    {
        path: "/addresslist",
        name: "AddressList",
        component: AddressList,
        meta: {
            title: "地址列表",
            isAuthRequired: true,
        },
    },
    {
        path: "/addressedit",
        name: "AddressEdit",
        component: AddressEdit,
        meta: {
            title: "地址详情",
            isAuthRequired: true,
        },
    },
    {
        path: "/createorder",
        name: "createOrder",
        component: createOrder,
        meta: {
            title: "订单预览",
            isAuthRequired: true,
        },
    },
];

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes,
});

router.beforeEach((to, from, next) => {
    // 如果没有登录，在这里到login
    if (to.meta.isAuthRequired && store.state.user.isLogin === false) {
        // alert('你还没有登录，请先登录')
        return next("/login");
    } else {
        next();
    }
    document.title = to.meta.title;
});

export default router;